package com.b2mining.integration.helper;

import java.io.File;
import java.util.Comparator;

/**
 * @since (7/3/2003 10:35:26 AM)
 * @author Synopsis S.A. <BR>
 * Analista Programador: Janyn Roca
 * 						
 */
public class FileComparator implements Comparator {
    public static final int ASCENDING = 0;
    public static final int DESCENDING = 1;
    private int c;
    
    public int getSortDirection()
    {
        return c;
    }

    public void setSortDirection(int i)
    {
        c = i;
    }
    
    protected int compareWhichType(int i)
    {
        if(getSortDirection() == 0)
            return compareAscending(i);
        else
            return compareDescending(i);
    }

    public final int compareDescending(int i)
    {
        return -1 * i;
    }

    public final int compareAscending(int i)
    {
        return i;
    }

    protected int compareNulls(Object obj, Object obj1)
    {
        if(obj == null && obj1 == null)
            return 0;
        if(obj == null)
            return compareWhichType(-1);
        else
            return compareWhichType(1);
    }
    
	/**
	 * @see java.util.Comparator#compare(Object, Object)
	 */
    public int compare(Object obj, Object obj1)
    {
        if(obj == null || obj1 == null)
            return compareNulls(obj, obj1);
        File file = (File)obj;
        File file1 = (File)obj1;
        long l = file.lastModified();
        long l1 = file1.lastModified();
        byte byte0 = 0;
        if(l > l1)
            byte0 = 1;
        else
        if(l < l1)
            byte0 = -1;
        return compareWhichType(byte0);
    }
    

}
